package com.example.testdownload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import com.coe3.datahandler.BaseActivity;

import android.util.Log;

public class Decompress {


	  private String _zipFile; 
	  private String _location;
	  private MainActivity _activity;

	  public Decompress(MainActivity activity, String zipFile, String location) { 
	    _zipFile = zipFile; 
	    _location = location; 
	    _activity = activity;

	    _dirChecker(""); 
	  } 

	  @SuppressWarnings("deprecation")
	  public void unzip() { 
	    try  { 
	    	_activity.showDialog( BaseActivity.DIALOG_CONNECTION_START );
		    	
			FileInputStream fin = new FileInputStream(_zipFile); 
			ZipInputStream zin = new ZipInputStream(fin); 
			ZipEntry ze = null; 
			while ((ze = zin.getNextEntry()) != null) { 
				Log.v("Decompress", "Unzipping " + ze.getName()); 
				
				if(ze.isDirectory()) { 
					_dirChecker(ze.getName()); 
				} else { 
					FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
					for (int c = zin.read(); c != -1; c = zin.read()) { 
						fout.write(c); 
					} 
				
					zin.closeEntry(); 
					fout.close(); 
				} 
			
			}
			
			_activity.dismissDialog(BaseActivity.DIALOG_CONNECTION_START );
			zin.close(); 
		} catch(Exception e) { 
			Log.e("Decompress", "unzip", e); 
		} 
		
	  } 

	  private void _dirChecker(String dir) { 
	    File f = new File(_location + dir); 

	    if(!f.isDirectory()) { 
	      f.mkdirs(); 
	    } 
	  } 
}
